package net.vkits.common.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created by ethan-liu on 2016/11/28.
 */
public class AlipayConfig {
    public static final String APP_ID = "2016111402803108";
    public static final String APP_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCj/LtUppGznHF4BAufI/y63NIqB8EEX7S6bkcqiYG/fn8K/2G/uQI4t5vJ3NVu99K5bBbp28mwSahGrxoZPDexvkCgRxC0y0CIkNxOekPk5VPTX34+dEq2bXA1zPW+VYIM+rYk/AazfInRUJ6+P/6OHSjvKG1iTAQn1F3B+wKOSQIDAQAB";
    public static final String APP_PRIVATE_KEY = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKP8u1SmkbOccXgE" +
            "C58j/Lrc0ioHwQRftLpuRyqJgb9+fwr/Yb+5Aji3m8nc1W730rlsFunbybBJqEav" +
            "Ghk8N7G+QKBHELTLQIiQ3E56Q+TlU9Nffj50SrZtcDXM9b5Vggz6tiT8BrN8idFQ" +
            "nr4//o4dKO8obWJMBCfUXcH7Ao5JAgMBAAECgYBb9/rL39spI10T1Rj7eIBeAEgx" +
            "4EVlAZPnVr+84H+O+Q+hCFc24jiXvIm3rY1Z/c0FxtQW9ffoUXb5pWx6qKYGuHaC" +
            "1Z37gHRO1HCma17ObQ2+mNLz78l00fmCHj+7hVOQj+f2QGf4Q3P7eHMvSaIWA4Dh" +
            "JYFOguhaxKkJ3mQfAQJBANABwQQ6YWrhIjqwnd6bwGaAgPT02z2XvjZD5LQpFc+x" +
            "A92pcGYkNisiWw7PysJ/FaOFc2SY2wZvpBDjrT0j9SkCQQDJ0uQbnIPZnxJ46nhh" +
            "DvDkdUhMiM+lryte6Afo8Zap+LH/mtGnrcmRexU+t+RHpEMssInTH0xIjFVCBqjg" +
            "ytQhAkBQ+N3Sb4eMu6c/gZFzHt5/CHcq5EaUa4W4SiW1nGjJOPdwSo1etaoAkJcx" +
            "5OHhZOdfiIurFXXtw3fyuy6tJlpBAkBliEztfx3tu1MFtZ995JBXvpDyv9kG10sQ" +
            "Ydao3/EV7WFO38odexZus+cKOGUFjaOlteKAbbxouwChSMxo2mFBAkB/IHGQIKo0" +
            "ypqJkUrrRl0VmuxyAO8CaMD7mPH5PfhjdL2me0j5wQ/mHEQR5wSMu+LbPHs9PlZx" +
            "SZHMuC6i8WdZ";
        public static final String CHARSET = "utf-8";
//    public static final String CHARSET = "GBK";
    public static final String ALIPAY_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB";
    public static final String METHOD = "alipay.trade.app.pay";
    public static final String signType = "RSA";
    public static final String version = "1.0";
    public static final String notify_url = "http://api.anlewowo.com/payment/notify";
    // 必须的业务参数
    // 商品的标题/交易标题/订单标题/订单关键字等
    private static final String subject = "安家喵房租";
    // 商户网站唯一订单号
//    private static final String out_trade_no = "12353123124";
    // 订单总金额，单位为元，精确到小数点后两位，取值范围[0.01,100000000]
//    private static final String total_amount = "10.00";
    private static final String body = "信用卡交租";
    private static final String product_code = "QUICK_MSECURITY_PAY";
    // 收款支付宝用户ID。 如果该值为空，则默认为商户签约账号对应的支付宝用户ID
    public static final String seller_id = "2088521164941274";
    // 该笔订单允许的最晚付款时间，逾期将关闭交易。
    // 取值范围：1m～15d。m-分钟，h-小时，d-天，1c-当天（1c-当天的情况下，无论交易何时创建，都在0点关闭）
    // 该参数数值不接受小数点， 如 1.5h，可转换为 90m
    private static final String timeout_express = "90m";

    public static String sign(String content) {
        try {
            return AlipaySignature.rsaSign(content, APP_PRIVATE_KEY, CHARSET);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getOriginalContent(String out_trade_no, String total_amount) {
        String timestamp = getTimestamp();
        String bizContent = getBizContent(subject, body, out_trade_no, total_amount);
        String originalContent = "app_id=" + APP_ID + "&biz_content=" + bizContent + "&charset=" + CHARSET + "&method=" + METHOD
                + "&notify_url=" + notify_url + "&sign_type=" + signType + "&timestamp=" + timestamp + "&version="
                + version;
        return originalContent;
    }

    public static String getContent(String sign, String out_trade_no, String total_amount) {
        String timestamp = getTimestamp();
        String bizContent = getBizContent(subject, body, out_trade_no, total_amount);
        try {
            String content = "app_id=" + APP_ID + "&biz_content=" + URLEncoder.encode(bizContent, CHARSET) + "&charset=" + CHARSET + "&method=" + METHOD
                    + "&notify_url=" + URLEncoder.encode(notify_url, CHARSET) + "&sign_type=" + signType + "&timestamp=" + URLEncoder.encode(timestamp, CHARSET) + "&version="
                    + version + "&sign=" + URLEncoder.encode(sign, CHARSET);
            return content;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }



    private static String getBizContent(String subject, String body, String outTradeNo, String totalAmount) {
        String context = "";
        context += "{" + "\"timeout_express\":\"" + timeout_express + "\"," + "\"seller_id\":\""
                + seller_id + "\"," + "\"product_code\":\"" + product_code + "\","
                + "\"total_amount\":\"" + totalAmount + "\"," + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\"," + "\"out_trade_no\":\"" + outTradeNo + "\"}";
        return context;
    }

    private static String getTimestamp() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}
